﻿using AuthCenter.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthCenter.Application
{
    public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity
    {
        private readonly MyContext db;
        public BaseRepository(MyContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Create(T t)
        {
            db.Set<T>().Add(t);
            return db.SaveChanges();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public int Create(List<T> values)
        {
            db.Set<T>().AddRange(values);
            return db.SaveChanges();
        }
        /// <summary>
        /// 查全部
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>();
        }
        /// <summary>
        /// 根据编号查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetModel(long id)
        {
            return db.Set<T>().Find(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Update(T t)
        {
            db.Set<T>().Update(t);
            return db.SaveChanges();
        }
    }
}
